package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.content.Intent
import android.util.Log
import com.darsh.multipleimageselect.activities.AlbumSelectActivity
import com.darsh.multipleimageselect.helpers.Constants
import com.dgshanger.sanhxiaofeisc.MainActivity
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.AdditionImageWrapper
import com.dgshanger.sanhxiaofeisc.mvp.contract.GradeContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.GradePresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.home.utils.PictureSelectDialog
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_grade.*
import java.util.*

/**
 * Created by JacobHHH on 2018/4/22.
 * 评分
 */
class GradeActivity : BaseMvpActivity<GradeContract.Model,GradeContract.View,GradePresenter>(),GradeContract.View {

    private var mAdditionImageWrapper: AdditionImageWrapper? = null
    private var mPictureSelectDialog: PictureSelectDialog? = null
    /*预览图片请求码，因为在预览时可以对图片进行删除操作。所以要接收返回回来的新数据*/
    private val REQUEST_VIEWER_IMG = 757
    private var images = ArrayList<String>()
    private var resultImages: MutableList<String> = ArrayList()
    private lateinit var mOrder_sn : String //订单号

    override fun createPresenter(): GradePresenter = GradePresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_grade

    override fun initData() {
        mOrder_sn=intent.getStringExtra("order_sn")
        val imageUrl = intent.getStringExtra("image")
        val name = intent.getStringExtra("name")
        val address = intent.getStringExtra("address")
        ImageLoader.loadToUrl(mContext,iv_shop,com.dgshanger.sanhxiaofeisc.common.Constants.getBaseUrl()+imageUrl)
        tv_shop.text=name
        tv_address.text=address

        mAdditionImageWrapper = AdditionImageWrapper(this, recyclerView)
        mAdditionImageWrapper!!.wrap(MyOnAdditionLayoutItemClick())
    }

    private var starCount : String = "0.0"
    override fun initEvent() {
        //星星
        rb.setOnRatingChangeListener{
            starCount=(it.toInt()*2).toDouble().toString()
            tv_grade.text=starCount
        }

        //提交评价
        btn_confirm.setOnClickListener{
            val content = ed_content.text.toString().trim()
            getPresenter().submitComment(mOrder_sn,starCount,content,resultImages)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Constants.REQUEST_CODE && null != data) {
            //图片选择返回
            images = data.getStringArrayListExtra(Constants.INTENT_EXTRA_IMAGES)
            //Log.d(TAG, "新选择的图片数量:" + images.size());
            //Log.d(TAG, "新选择的图片路径:" + images.get(0));
            getPresenter().imageSelect(images)

        } else if (requestCode == REQUEST_VIEWER_IMG && null != data) {
            val imageList = data.getStringArrayListExtra(ImageViewerAndDeleteActivity.IMGS_DATA_KEY)
            Log.e("Tag","删除的图片="+imageList)
            getPresenter().imageDelete(imageList)
        }
    }

    override fun setImageResult(pictures: MutableList<String>?) {
        resultImages = pictures!!
        mAdditionImageWrapper!!.setData(resultImages)
    }

    /**
     * RecyclerView item点击
     */
    private inner class MyOnAdditionLayoutItemClick : AdditionImageWrapper.OnAdditionLayoutItemClick {
        override fun onAdditionItemClick() {
            //添加的item被点击
            showMultiImageDialog()
        }

        override fun onImageItemClick(position: Int) {
            //普通图片被点击
            launcherToImageViewer(position)
        }

        override fun onImageDeleteClick(position: Int) {
            resultImages.removeAt(position)
            mAdditionImageWrapper!!.setData(resultImages)
        }
    }

    /**
     * 显示图片选择对话框
     */
    private fun showMultiImageDialog() {
        if (null == mPictureSelectDialog) {
            mPictureSelectDialog = PictureSelectDialog(this)
            mPictureSelectDialog!!.setOnPictureSelectDialogClickListener(MyOnPictureSelectDialogClickListener())
        }
        mPictureSelectDialog!!.show()
    }

    /**
     * 跳转到图片预览界面
     *
     * @param position 要显示的页面的索引
     */
    private fun launcherToImageViewer(position: Int) {
        val intent = Intent(this, ImageViewerAndDeleteActivity::class.java)
        val imageList = mAdditionImageWrapper!!.getImageList()
        intent.putStringArrayListExtra(ImageViewerAndDeleteActivity.IMGS_DATA_KEY, imageList as ArrayList<String>)
        intent.putExtra(ImageViewerAndDeleteActivity.INDEX_KEY, position)
        intent.putExtra(ImageViewerAndDeleteActivity.SHOW_DELETE_BTN, true)
        startActivityForResult(intent, REQUEST_VIEWER_IMG)
    }

    /**
     * 图片选择对话框回调接口
     */
    private inner class MyOnPictureSelectDialogClickListener : PictureSelectDialog.OnPictureSelectDialogClickListener {
        override fun onCamera() {
            launcherToMultiImageSelect(true)
        }

        override fun onPhoto() {
            launcherToMultiImageSelect(false)
        }
    }

    /**
     * 跳到图片多选页面
     *
     * @param goToCamera 是否直接跳到拍照
     */
    private fun launcherToMultiImageSelect(goToCamera: Boolean) {
        val intent = Intent(mContext, AlbumSelectActivity::class.java)
        intent.putExtra("limit", mAdditionImageWrapper!!.getRemainCount())
        intent.putExtra(AlbumSelectActivity.KEY_GO_TO_CAMERA_ACTION, goToCamera)
        startActivityForResult(intent, 2000)
    }

    override fun submitCommentSuccess(msg: String?) {
        showToast(msg)
        finish()
        startActivity(MainActivity::class.java)
    }
}